<!--地址管理表单-->
<template>
  <div class="contain">
    <app-header></app-header>
    <div class="form-wrap mb30">
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>收货人</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="contactName"/></div>
        </div>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>联系电话</div>
          <div class="ipt flex1"><input type="text" placeholder="点击输入" v-model="contactTel"/></div>
        </div>
      </div>
      <div class="form-choice line">
        <x-address  class="vux-form-require"
                    title="所在地区" 
                    v-model="addrvalue" 
                    :list="addressData" 
                    value-text-align="left"
                    placeholder="点击选择"></x-address>
      </div>
      <div class="form-wrap-row">
        <div class="line displayflex">
          <div class="label"><span class="red">*</span>详细地址</div>
          <div class="ipt flex1">
            <textarea class="form-txtarea" type="text" placeholder="请输入详细的街道地址" v-model="address"></textarea>
          </div>
        </div>
      </div>
    </div>
    <div class="btn-save" @click="saveAddr">保存</div>
  </div>
</template>

<script>
import appHeader from '@/components/core/header/app_header.vue';
export default {
  components:{
    appHeader
  },
  name:'addressForm',
  data() {
    return {
      contactName:'',
      contactTel:'',
      addrvalue:[],
      addressData: this.ChinaAddressV3Data,
      provine:'',
      city:'',
      district:'',
      address:'',
    }
  },
  computed:{
    editAddr(){
      return this.$route.query.Guid?true:false;
    },
    userinfo(){
      return localStorage.getItem('userinfo') === null?null:JSON.parse(localStorage.getItem('userinfo'));
    },
  },
  created(){
    if(this.editAddr){
      this.getDate();
    }
  },
  methods:{
    getDate(){
      var self=this;
      //编辑
      var query = {
          Guid: this.$route.query.Guid,
      };
      this.Ajax.post('SearchCustomerAddress', query).then(function(resp) {
        if (resp.IsSuccess == true) {
          var addresslist = resp.Datas[0];
          self.provine = addresslist.Province;
          self.city = addresslist.City;
          self.district = addresslist.District;
          self.address = addresslist.Address;
          self.contactName = addresslist.Contacts;
          self.contactTel = addresslist.ContactsTel;
          self.addrvalue.push(self.provine,self.city,self.district);
        }
      });
    },
    saveAddr(){
      if(!this.contactName){
        this.Toast({
          message: '请输入收货人姓名'
        })
        return false;
      }
      var testTel = /^1[345789]\d{9}$/;
      if (!this.contactTel || !testTel.test(this.contactTel)) {
        this.Toast({
            message: '请输入正确格式的联系电话'
        });
        return false;
      };
      if(this.addrvalue.length==0){
        this.Toast({
          message: '请选择省市区'
        })
        return false;
      }
      if(!this.address){
        this.Toast({
          message: '请填写具体地址'
        })
        return false;
      }
      var self = this,
          methods='',
          param='',
          userinfo = JSON.parse(localStorage.userinfo);
      var query = {
            Province: this.addrvalue[0],
            City: this.addrvalue[1],
            District:this.addrvalue[2],
            Address: this.address,
            contacts:this.contactName,
            contactsTel:this.contactTel,
            CCGUID:this.$route.query.ccguid,
            CreateUserId: this.userinfo.StaffID,
            CreateUserName: this.userinfo.Name,
            UpdateUserId: this.userinfo.StaffID,
            UpdateUserName: this.userinfo.Name,
          };
      if (this.editAddr) {
        methods = 'UpdateCustomerAddress';
        query.GUID = this.$route.query.Guid;
      }else{
        methods = 'AddCustomerAddress';
        //query.IsChecked = 'N';
      }
      this.Ajax.post(methods, query).then(function(resp) {
        if (resp.IsSuccess) {
          self.$router.go(-1);
        }else{
          self.Toast({
            message: resp.ErrorMessage||'操作失败，请稍后重试'
          })
        }
      });
    }
  }
}
</script>